package com.aaa.sbms.dao;

import com.sun.istack.internal.Interned;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * fileName:NewsDao
 * description:
 * author:zz
 * createTime:2019/11/26 14:12
 * version:1.0.0
 */
public interface NewsDao {

    /**
     * 新闻列表
     * @return
     */
    @Select("select * from news")
    List<Map>  getList();

    /**
     * 带参分页查询新闻
     * @param map
     * @return
     */
//    @Select("<script>select * from (\n" +
//            "  select rownum rn,t.* from (\n" +
//            "   select * from tb_news  <where>" +
//            " <if test='title!=null'> and title like '%${title}%' </if>"+
//            " <if test=\"clickNum!=null and clickNum !=''\"> and clicknum =#{clickNum} </if>"+
//            " <if test=\"typeId!=null and typeId != ''\"> and type_id =#{typeId} </if>"+
//            "</where> order by id desc\n" +
//            "  ) t where    rownum &lt; #{end}  \n" +
//            ") where rn &gt; #{begin}    </script>")
    @Select("<script>select n.newsid,n.newstid,n.title,n.stick,n.thick,n.redsign,n.clicknum,n.imagepath,date_format(n.addtime,'%Y%m%d %h:%i:%s') addtime,u.user_name from news n\n"+
            "join sys_user u on u.user_id=n.userid"+
            "<where>" +
            " <if test='title!=null'> and title like '%${title}%' </if>"+
            " <if test=\"clicknum!=null and clicknum !=''\"> and clicknum =#{clicknum} </if>"+
            " <if test=\"newstid!=null and newstid != ''\"> and newstid =#{newstid} </if>"+
            "</where>order by n.newsid desc\n" +
            "limit #{begin},#{end}</script>")
    List<Map>  getPage(Map map);

    /**
     * 分页查询新闻附件
     * @param map
     * @return
     */
    @Select("<script>select * from news_accessory <where>" +
            " <if test='accname!=null'> and accname like '%${accname}%' </if>"+
            " <if test=\"accid!=null and accid !=''\"> and accid =#{accid} </if>"+
            " <if test=\"newsid!=null and newsid != ''\"> and newsid =#{newsid} </if>"+
            "</where> limit #{begin},#{end}</script>")
    List<Map> newsAccPage(Map map);

    /**
     * 带参总数量查询新闻
     * @param map
     * @return
     */
//    @Select("<script>select count(*) from tb_news  <where>" +
//            " <if test='title!=null'> and title like '%${title}%' </if>"+
//            " <if test=\"clickNum!=null and clickNum !=''\"> and clicknum =#{clickNum} </if>"+
//            " <if test=\"typeId!=null and typeId != ''\"> and type_id =#{typeId} </if>"+
//            "</where></script>")
    @Select("<script>select count(*) from news  <where>" +
            " <if test='title!=null'> and title like '%${title}%' </if>"+
            " <if test=\"clicknum!=null and clicknum !=''\"> and clicknum =#{clicknum} </if>"+
            " <if test=\"newstid!=null and newstid!= ''\"> and newstid=#newstid</if>"+
            "</where></script>")
    int getPageCount(Map map);

    /**
     * 新闻附件总数量查询
     * @param map
     * @return
     */
    @Select("<script>select count(*) from news_accessory  <where>" +
            " <if test='accname!=null'> and accname like '%${accname}%' </if>"+
            " <if test=\"accid!=null and accid !=''\"> and accid =#{accid} </if>"+
            " <if test=\"newsid!=null and newsid != ''\"> and newsid =#{newsid} </if>"+
            "</where></script>")
    int newsAccPageCount(Map map);

    /**
     * 查询一条新闻的数据
     * @return
     */
    @Select("select * from news where newsid=#{newsid}")
    Map newsOne(int newsid);

    /**
     * 新闻类型
     * @return
     */
    @Select("select * from newstype")
    List<Map> newstypeList();

    /**
     * 一条新闻类型
     * @param newstid
     * @return
     */
    @Select("select * from newstype where newstid=#{newstid}")
    Map newstypeOne(int newstid);

    /**
     * 一条新闻附件
     * @param accid
     * @return
     */
    @Select("select * from news_accessory where accid=#{accid}")
    Map newsAccOne(int accid);

    /**
     * 添加新闻
     * @param map
     * @return
     */
    @Insert("insert into news(newstid,title,intro,stick,thick,redsign,mainbody,addtime,imagename,imagepath,userid) " +
            "values(#{newstid},#{title},#{intro},#{stick},#{thick},#{redsign},#{mainbody},date_format(now(),'%Y-%m-%d %h:%i:%s'),#{imagename},#{imagepath},#{userid})")
    int add(Map map);

    /**
     * 添加新闻类型
     * @param map
     * @return
     */
    @Insert("insert into newstype(newstype,intro) values(#{newstype},#{intro})")
    int addNewstype(Map map);

    /**
     * 添加新闻附件
     * @param map
     * @return
     */
    @Insert("insert into news_accessory(newsid,accname,accpath,accsize) " +
            "values(#{newsid},#{accname},#{accpath},#{accsize})")
    int addNewsAcc(Map map);

    /**
     * 更新新闻列表
     * @param map
     * @return
     */
    @Update("update news set title=#{title},intro=#{intro},stick=#{stick},thick=#{thick}," +
            "redsign=#{redsign},mainbody=#{mainbody},addtime=date_format(now(),'%Y-%m-%d %H:%i:%s')," +
            "imagename=#{imagename},imagepath=#{imagepath},userid=#{userid} where newsid=#{newsid}")
    int update(Map map);

    /**
     * 更新新闻类型
     * @param map
     * @return
     */
    @Update("update newstype set newstype=#{newstype},intro=#{intro} where newstid=#{newstid}")
    int updateNewstype(Map map);

    /**
     * 更新新闻附件
     * @param map
     * @return
     */
    @Update("update news_accessory set newsid=#{newsid},accname=#{accname},accpath=#{accpath},accsize=#{accsize}" +
            " where accid=#{accid}")
    int updateNewsAcc(Map map);

    /**
     * 根据编号删除新闻
     * @param newsid
     * @return
     */
    @Delete("delete from news where newsid=#{newsid}")
    int delete(int newsid);

    /**
     * 根据编号删除新闻类型
     * @param newsid
     * @return
     */
    @Delete("delete from newstype where newstid=#{newstid}")
    int deleteNewstype(int newsid);

    /**
     * 根据编号删除新闻附件
     * @param accid
     * @return
     */
    @Delete("delete from news_accessory where accid=#{accid}")
    int deleteNewsAcc(int accid);

    //                                         --前台--

    /**
     * 新闻列表
     * @return
     */
    @Select("select newsid,title,stick,\n" +
            "date_format(addtime,'%Y-%m-%d %H:%i:%s') addtime from news\n" +
            "order by stick desc,addtime desc")
    List<Map> newsList();

    /**
     * 根据id查询一条新闻内容
     * @param newsid
     * @return
     */
    @Select("select newsid,title,mainbody,imagepath from news where newsid=#{newsid}")
    Map newsOne2(int newsid);
}